package com.example.tang.zhayan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tang.zhayan.R;
import com.example.tang.zhayan.adapter.SplashImgAdapter;
import com.example.tang.zhayan.utils.AnimationUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tang on 2015/12/12.
 * 滑动界面
 */
@ContentView(R.layout.activity_splash)
public class SplashActivity extends AppCompatActivity {
    @ViewInject(R.id.viewPager_splash)
    private ViewPager viewPager;
    @ViewInject(R.id.rg_splash)
    private RadioGroup radioGroupSplash;
    //跳转按钮
    @ViewInject(R.id.tv_seek_to)
    private TextView textView;
    //滑动图片资源
    private int[] imgs = new int[]{R.mipmap.guidance_one_icon, R.mipmap.guidance_two_icon,
            R.mipmap.guidance_three_icon, R.mipmap.guidance_four_icon};
    //滑动界面图片集合
    private List<ImageView> listImg;
    //滑动界面适配器
    private SplashImgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        createData();
        adapter = new SplashImgAdapter(listImg);
        viewPager.setAdapter(adapter);
        //viewPager的监听事件
        addListener();
    }

    private void addListener() {
        /**
         * 跳转点击事件
         */
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });


        /**
         * viewPager的滑动监听事件
         */
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < radioGroupSplash.getChildCount(); i++) {
                    RadioButton radioButton = (RadioButton) radioGroupSplash.getChildAt(i);
                    radioButton.setChecked(false);
                }
                RadioButton radioButton = (RadioButton) radioGroupSplash.getChildAt(position);
                radioButton.setChecked(true);
                if (position == 3) {
                    textView.setVisibility(View.VISIBLE);
                } else {
                    textView.setVisibility(View.GONE);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 滑动界面图片数据
     */
    private void createData() {
        listImg = new ArrayList<>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView imageView = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.
                    MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(imgs[i]);
            listImg.add(imageView);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        this.finish();
    }
}
